<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/12 0012
 * Time: 17:39
 */
namespace app\api\controller;
use app\common\model\BaseModel;
use app\common\model\Distributionrecord;
use app\common\model\Querydeliverfee;
use think\Debug;
use think\Log;
use think\Request;

class DadaNotify extends BaseController
{

    private function queryStatusName($result)
    {

        switch ($result['statusCode']) {
            case 1:
                return "等待起手接单";
            case 2:
                return "待取货";
            case 3:
                return "配送中";
            case 4:
                return "已完成";
            case 5:
                return "已取消";
            case 7:
                return "已过期";
            case 8:
                return "指派单";
            case 9:
                return "妥投异常之物品返回中";
            case 10:
                return "妥投异常之物品返回完成";
            case 1000:
                return "创建达达运单失败";
        }
    }

    private function getdadaRecordParam($data){
        $param['order_id'] = $data['order_id'];
        $param['statusCode'] = $data['statusCode'];
        $param['transporterName'] = $data['transporterName'];
        $param['transporterPhone'] = $data['transporterPhone'];
        $param['transporterLng'] = $data['transporterLng'];
        $param['transporterLat'] = $data['transporterLat'];
        $param['deliveryFee'] = $data['deliveryFee'];
        $param['tips'] = $data['tips'];
        $param['receiptUrl'] = $data['receiptUrl'];
        $param['createTime'] = $data['createTime'];
        $param['acceptTime'] = $data['acceptTime'];
        $param['fetchTime'] = $data['fetchTime'];
        $param['finishTime'] = $data['finishTime'];
        $param['cancelTime'] = $data['cancelTime'];
        $param['orderFinishCode'] = $data['orderFinishCode'];
        $param['insuranceFee'] = $data['insuranceFee'];
        $param['statusName'] = $data['statusName'];
/*
  'supplierName' => '测试门店',
  'supplierAddress' => '上海市隆宇大厦',
  'supplierPhone' => '15811112222',
  'supplierLat' => '31.230238',
  'supplierLng' => '121.515559',*/
        return $param;
    }

        //存储物流跟踪
        public function dadaDistributionRecord($order_id,$order_status){
            $data['order_id'] = $order_id;
            $data['statusCode'] = $order_status;
            $DadaAddOrder = new \distribution\DadaAddOrder();
            $param['order_id'] = $order_id;
            $result = $DadaAddOrder->orderQuery($param);

            Log::record('orderQuery----------------------------------- ' . var_export($result, true), 'info');

            if($result['code'] == 0) {
                $dadaResult = $result['result'];
                Log::record('dadaResult----------------------------------- ' . var_export($dadaResult, true), 'info');
                $queryStatusName = $this->queryStatusName($dadaResult);
                $result['result']['statusName'] = $queryStatusName;
                $result['result']['order_id'] = $result['result']['orderId'];
                unset($result['result']['orderId']);
                //哒哒配送订单追踪
                $distributionRecordModel = new Distributionrecord();
                //查询当前订单与当前订单状态是否存在 存在就修改，不存在就保存
                $findDistributionRecord = $distributionRecordModel->findDistributionRecord($data);

                //获取达达配送参数
                $resultData = $this->getdadaRecordParam($result['result']);
                Log::record('resultData----------------------------------- ' . var_export($resultData, true), 'info');

                if($findDistributionRecord && !empty($findDistributionRecord)){
                    $id = $findDistributionRecord->id;
                    $updateResult = $distributionRecordModel->updateDistributionRecord($resultData,$id);
                    Log::record('updateResult----------------------------------- ' . var_export($updateResult, true), 'info');
                    return $updateResult;
                }else{
                    $insertResult = $distributionRecordModel->insertDistributionRecord($resultData);
                    Log::record('insertResult----------------------------------- ' . var_export($insertResult, true), 'info');
                    return $insertResult;
                }
            }
        }

        private function editOrderStatus($order_status){
           /*
            * 达达订单状态
            const status_1 = 1; //订单状态(待接单＝1
            const status_2 = 2; // 待取货＝2
            const status_3 = 3; // 配送中＝3
            const status_4 = 4; // 已完成＝4
            const status_5 = 5; // 已取消＝5
            const status_7 = 7; // 已过期=7
            const status_8 = 8; // 指派单=8
            const status_9 = 9; // 妥投异常之物品返回中=9
            const status_10 = 10; // 妥投异常之物品返回完成=10
            const status_1000 = 1000; // 创建达达运单失败=1000
            order订单状态
            订单状态
            1=待付款，
            2=待发货，
            3=待收货，
            4=已完成，
            5=已失效，
            6=售后，
            7=已消费，
            8=未消费，
            9=拼团中，
            10=同意售后退款，
            11=拒绝售后退款,
            12=待接单/已派送,
            13=已取消*/
           $orderModel = new \app\common\model\Order();
            switch ($order_status){
                case 1:
                    $status = $orderModel::STATUS_12;
                    break;
                case 2:
                    $status = $orderModel::STATUS_PENDING_DELIVERY;
                    break;
                case 3:
                    $status = $orderModel::STATUS_3;
                    break;
                case 4:
                    $status = $orderModel::STATUS_4;
                    break;
                case 5:
                    $status = $orderModel::STATUS_13;
                    break;
                case 7:
                    $status = $orderModel::STATUS_5;
                    break;
                case 8:
                    $status = $orderModel::STATUS_PENDING_DELIVERY;
                    break;
                case 9:
                    $status = $orderModel::STATUS_PENDING_DELIVERY;
                    break;
                case 10:
                    $status = $orderModel::STATUS_PENDING_DELIVERY;
                    break;
                case 1000:
                    $status = $orderModel::STATUS_5;
            }
            return $status;
        }

        public function index(){
            $client_id = input('client_id');
            $cancel_from = input('cancel_from');
            $order_status = input('order_status');
            $cancel_reason = input('cancel_reason');
            $signature = input('signature');
            $dm_id = input('dm_id');
            $dm_name = input('dm_name');
            $dm_mobile = input('dm_mobile');
            $order_id = input('order_id');
            $queryDeliverfeeModel = new Querydeliverfee();
            $findQueryDeliver = $queryDeliverfeeModel->findQueryDeliver($order_id);
            $data['client_id'] = $client_id;
            $data['cancel_from'] = $cancel_from;
            $data['order_status'] = $order_status;
            $data['cancel_reason'] = $cancel_reason;
            $data['signature'] = $signature;
            $data['dm_id'] = $dm_id;
            $data['dm_name'] = $dm_name;
            $data['dm_mobile'] = $dm_mobile;
            $data['order_id'] = $order_id;
            Log::record('savename ' . var_export($data, true), 'info');
            if($findQueryDeliver && !empty($findQueryDeliver)){
                $result = $queryDeliverfeeModel->updateQueryDeliver($data);
                $status = $this->editOrderStatus($order_status);
                $orderModel = new \app\common\model\Order();
                $orderParam['orderId'] = $order_id;
                $orderParam['status'] = $status;
                $orderStatus = $orderModel->updateOrderStatus($orderParam);
                Log::record('orderStatus----------------------------------- ' . var_export($orderStatus, true), 'info');
                    //哒哒订单追踪
                $this->dadaDistributionRecord($data['order_id'],$data['order_status']);
            }
        }




}